// pages/loginByAccount/loginByAccount.js
const util = require('../../utils/util')
const { AuthLoginByAccount, AuthLogout, AuthRegister, AuthReset } = require('../../config/api')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    account: '',
    password: '',
    needback:0,
  },
  // 跳转注册账号页面
  goRegister() {
    wx.navigateTo({
      url: '../register/register',
    })
  },
  // 清空输入框
  clearInput(e) {
    if (e.currentTarget.dataset.type == 'account') {
      this.setData({
        account: ''
      })
    } else {
      this.setData({
        password: ''
      })
    }
  },

  // 实时获取账号输入框数据
  changeAccValue(e) {
    this.setData({
      account: e.detail.value
    })
  },
  // 实时获取密码输入框数据
  changePswValue(e) {
    this.setData({
      password: e.detail.value
    })
  },
  // 账号登录按钮
  loginByAccount() {
    let that = this;
    if (this.data.account && this.data.password) {
      util.request(AuthLoginByAccount, {
        username: that.data.account,
        password: that.data.password
      }, 'POST').then(res => {
        if (res.errno == 0) {
          app.globalData.hasLogin=true;
          wx.setStorageSync('token', res.data.token);
          wx.setStorageSync('userInfo', res.data.userInfo)
          if(this.data.needback){
            wx.navigateBack({
              delta:2
            })
          }else{
            wx.switchTab({
              url: '../user/user',
            })
          }
          
        }else{
          util.showErrorToast(res.errmsg)
          this.setData({
            password:''
          })
        }

      }).catch(err => {
        console.log("err", err);
      })
    } else {
      util.showErrorToast('请输入账号密码！')
    }
  },






  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.needback==1){
      this.setData({
        needback:1
      })
    }
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },








})